using System;
using System.ComponentModel;
using System.Transactions;

using SIS.ENT;
using SIS.DAL;
using System.Data.SqlClient;
using System.Configuration;


/// <summary>
/// The EmployeeProfileTableManager class is responsible for managing BO.EmployeeProfileTable objects in the system.
/// </summary>
[DataObjectAttribute()]
public class EmployeeProfileTableManager
  {

#region "Public Methods"

 

/// </summary>
/// <returns>A list with all EmployeeProfileTable from the database when the database contains any, or null otherwise.</returns>
[DataObjectMethod(DataObjectMethodType.Select, true)]
public static EmployeeProfileTableList GetList(int SchoolKey)
{
    return EmployeeProfileTableDB.GetList(SchoolKey);
}

[DataObjectMethod(DataObjectMethodType.Select, true)]
public static EmployeeProfileTableList GetList(int SchoolKey, string Name, String Address, String NRICNO, Int16 AddressType, Int16 CountryCode, int CityCode, int DistrictCode)
{
    return EmployeeProfileTableDB.GetList(SchoolKey, Name, Address, NRICNO, AddressType, CountryCode, CityCode, DistrictCode);
}

/// <summary>
/// Gets a single EmployeeProfileTable from the database without its data.
/// </summary>
/// <param name="employeeProfileKey">The EmployeeProfileKey of the EmployeeProfileTable in the database.</param>
/// <returns>A EmployeeProfileTable object when the EmployeeProfileKey exists in the database, or <see langword="null"/> otherwise.</returns>
[DataObjectMethod(DataObjectMethodType.Select, false)]
public static EmployeeProfileTable GetItem(int employeeProfileKey){
return GetItem(employeeProfileKey, false);
}

[DataObjectMethod(DataObjectMethodType.Select, false)]
public static EmployeeProfileTable GetItem(int employeeProfileKey,int schoolkey)
{
    return GetItem(employeeProfileKey, schoolkey,false);
}

[DataObjectMethod(DataObjectMethodType.Select, false)]
public static EmployeeProfileTable GetItemByUserAccountKey(int UserAccountKey, int SchoolKey)
{
    EmployeeProfileTable myEmployeeProfileTable = EmployeeProfileTableDB.GetItemByUserAccountKey(UserAccountKey, SchoolKey);
    return myEmployeeProfileTable;
}

/// <summary>
/// Gets a single EmployeeProfileTable from the database.
/// </summary>
/// <param name="employeeProfileKey">The EmployeeProfileKey of the EmployeeProfileTable in the database.</param>
/// <param name="getEmployeeProfileTableRecords">Determines whether to load all associated EmployeeProfileTable records as well.</param>
/// <returns>
/// A EmployeeProfileTable object when the EmployeeProfileKey exists in the database, or <see langword="null"/> otherwise.
/// </returns>
[DataObjectMethod(DataObjectMethodType.Select, false)]
public static EmployeeProfileTable GetItem(int employeeProfileKey, bool getEmployeeProfileTableRecords){
EmployeeProfileTable myEmployeeProfileTable = EmployeeProfileTableDB.GetItem(employeeProfileKey);
return myEmployeeProfileTable;
}

[DataObjectMethod(DataObjectMethodType.Select, false)]
public static EmployeeProfileTable GetItem(int employeeProfileKey, int SchoolKey, bool getTeacherProfileTableRecords)
{
    EmployeeProfileTable myEmployeeProfileTable = EmployeeProfileTableDB.GetItem(employeeProfileKey, SchoolKey);
    if (myEmployeeProfileTable != null && getTeacherProfileTableRecords)
    {
        myEmployeeProfileTable.timeScheduleTables = TimeScheduleTableDB.GetListByTeacherProfileKey(employeeProfileKey);
    }
    return myEmployeeProfileTable;
}

/// <summary>
/// Saves a EmployeeProfileTable in the database.
/// </summary>
/// <param name="myEmployeeProfileTable">The EmployeeProfileTable instance to save.</param>
/// <returns>The new EmployeeProfileKey if the EmployeeProfileTable is new in the database or the existing EmployeeProfileKey when an item was updated.</returns>
[DataObjectMethod(DataObjectMethodType.Update, true)]
public static int Save(EmployeeProfileTable myEmployeeProfileTable){
using (TransactionScope myTransactionScope = new TransactionScope()){
int employeeProfileTableEmployeeProfileKey = EmployeeProfileTableDB.Save(myEmployeeProfileTable);

//  Assign the EmployeeProfileTable its new (or existing EmployeeProfileKey).
myEmployeeProfileTable.EmployeeProfileKey = employeeProfileTableEmployeeProfileKey;

myTransactionScope.Complete();

return employeeProfileTableEmployeeProfileKey;
}
}

/// <summary>
/// Deletes a EmployeeProfileTable from the database.
/// </summary>
/// <param name="myEmployeeProfileTable">The EmployeeProfileTable instance to delete.</param>
/// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
[DataObjectMethod(DataObjectMethodType.Delete, true)]
public static bool Delete(EmployeeProfileTable myEmployeeProfileTable){
return EmployeeProfileTableDB.Delete(myEmployeeProfileTable.EmployeeProfileKey);
}

[DataObjectMethod(DataObjectMethodType.Delete, true)]
public static bool Delete(int EmployeeProfileKey, int SchoolKey)
{
    SqlTransaction myTransaction = null;
    bool lResult = true;
    int count = 0;
    using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
    {
        try
        {
            myConnection.Open();
            myTransaction = myConnection.BeginTransaction();
            EmployeeProfileTable objProfile = new EmployeeProfileTable();
            objProfile = EmployeeProfileTableManager.GetItem(EmployeeProfileKey, SchoolKey);
            EmployeeQualtificationTableList objEmployeeQualification = new EmployeeQualtificationTableList();
            EmployeeOtherSkillsTableList objEmployeeOtherSkill = new EmployeeOtherSkillsTableList();
            EmployeeJobExperiencesTableList objEmployeeJobExp = new EmployeeJobExperiencesTableList();
            EmployeeDesignationTableList objEmployeeDesignation = new EmployeeDesignationTableList();
            EmployeeSalaryTableList objEmployeeSalary = new EmployeeSalaryTableList();

            int UserAccountKey = 0;
            if (objProfile != null)
            {
                UserAccountKey = objProfile.UserAccountKey;

            }
            lResult = EmployeeProfileTableDB.Delete(EmployeeProfileKey);
            objEmployeeDesignation = EmployeeDesignationTableManager.GetList(UserAccountKey);
            foreach (EmployeeDesignationTable item in objEmployeeDesignation)
            {
                if (EmployeeDesignationTableManager.Delete(item.EmployeeDesignationKey))
                {
                    count = count + 1;
                }
            }
            count = 0;
            objEmployeeSalary = EmployeeSalaryTableManager.GetList(UserAccountKey);
            foreach (EmployeeSalaryTable item in objEmployeeSalary)
            {
                if (EmployeeSalaryTableManager.Delete(item.EmployeeSalaryKey))
                {
                    count = count + 1;
                }
            }
            count = 0;
            objEmployeeQualification = EmployeeQualtificationTableManager.GetList(UserAccountKey);
            foreach (EmployeeQualtificationTable item in objEmployeeQualification)
            {
                if (EmployeeQualtificationTableManager.Delete(item.EmployeeQualtificationKey))
                {
                    count = count + 1;
                }
            }
            count = 0;
            objEmployeeOtherSkill = EmployeeOtherSkillsTableManager.GetList(UserAccountKey);
            foreach (EmployeeOtherSkillsTable item in objEmployeeOtherSkill)
            {
                if (EmployeeOtherSkillsTableManager.Delete(item.EmployeeOtherSkillsKey))
                {
                    count = count + 1;
                }
            }
            count = 0;
            objEmployeeJobExp = EmployeeJobExperiencesTableManager.GetListByUserAccountKey(UserAccountKey);
            foreach (EmployeeJobExperiencesTable item in objEmployeeJobExp)
            {
                if (EmployeeJobExperiencesTableManager.Delete(item.EmployeeJobExperienceKey))
                {
                    count = count + 1;
                }
            }
            myTransaction.Commit();

        }

        catch (Exception ex)
        {
            myTransaction.Rollback();
            throw ex;
        }
        finally
        {
            myConnection.Close();
        }
    }

    return lResult;

}

public static bool UpdatePhoto(int EmployeeProfileKey, Byte[] imgPhoto)
{
    return EmployeeProfileTableDB.UpdatePhoto(EmployeeProfileKey, imgPhoto);
}

public static Byte[] GetPhoto(int TeacherProfileKey)
{
    return TeacherProfileTableDB.GetPhoto(TeacherProfileKey);
}

#endregion

}

